//
//  JMenu.m
//  SpreadButton
//
//  Created by mysj on 16/6/30.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "JMenu.h"
//间距
#define DISTANCE  10
//动画时间
#define DURATION  0.15f

#import "UIView+Add.h"
@interface JMenu ()

@property (nonatomic, copy)  selected           myselected;
@property (nonatomic, strong) NSMutableArray *  items;
@property (nonatomic, assign) BOOL              iSdid;
@end


@implementation JMenu

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}

+ (instancetype)menuImg:(NSArray *)imgNames Selected:(selected)selected {
    
    JMenu *menu = [JMenu buttonWithType:UIButtonTypeCustom];
    [menu menuImg:imgNames Selected:selected];
    
    return menu;
}


- (void)menuImg:(NSArray *)imgNames Selected:(selected)selected {
    
    self.myselected =  selected;
    self.imgNames = imgNames;
    
    [self setImage:[UIImage imageNamed:@"pilotBtn_0.png"] forState:UIControlStateNormal];
    [self setImage:[UIImage imageNamed:@"pilotBtn_0h.png"] forState:UIControlStateHighlighted];
    [self addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchDown];
    self.alpha = 0.5;
    [self loadSubview];
}

- (void)loadSubview {
    
    _items = [[NSMutableArray alloc] initWithCapacity:self.imgNames.count];
    for (int i = 1; i < self.imgNames.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = self.frame;
        button.tag = i;
        [button setImage:[UIImage imageNamed:_imgNames[i]] forState:UIControlStateNormal];
        NSString *imgNameH = [NSString stringWithFormat:@"pilotBtn_%dh",i];
        [button setImage:[UIImage imageNamed:imgNameH] forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        if (i == 1) {
             [button setTitle:@"申请据点" forState:UIControlStateNormal];
        } else {
           [button setTitle:@"发起攻击" forState:UIControlStateNormal];
        }
        button.titleLabel.font = [UIFont systemFontOfSize:10];
        [_items addObject:button];
    }
}

- (void)itemClick:(UIButton *)item {
    
    [UIView animateWithDuration:DURATION animations:^{
        
        for (int i = 0; i < _items.count; i++) {
            UIButton *but = [_items objectAtIndex:i];
            but.frame = self.frame;
            but.alpha = 0;
        }
    } completion:^(BOOL finished) {
        
        for (int i = 0; i < _items.count; i++) {
            UIButton *but = [_items objectAtIndex:i];
            [but removeFromSuperview];
        }
    }];
    
    if (self.myselected && item) {
        self.myselected(item.tag);
    }
    self.iSdid = NO;
    self.alpha = .5;
}

- (void)buttonClick {
    
    if (self.iSdid) {
        [self itemClick:nil];
        return;
    }
    
    
    
    for (int i = 0; i < _items.count; i++) {
        
        UIButton *btn = [_items objectAtIndex:i];
        btn.alpha = 1;
        btn.frame = self.frame;
        btn.alpha = .3;
        [self.superview addSubview:btn];
        
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, -3, 0, 0);
        CGFloat y = (self.frame.origin.y - self.frame.size.height - DISTANCE)  + -(DISTANCE + self.frame.size.height) * i + 1;
        CGFloat w = 71;
        CGFloat h = self.frame.size.height;
        
        [UIView animateWithDuration:DURATION animations:^{
            btn.alpha = 1;
            btn.frame = CGRectMake(5, y, w, h);
        }];
    }
    
    self.alpha = 1;
    self.iSdid = YES;
}

@end
